package com.pro.common.service.sitecontent.controller;

import com.pro.common.api.enums.init.EnumCurrency;
import com.pro.common.api.sitecontent.model.db.CurrencyExchangeRate;
import com.pro.common.api.sitecontent.model.dto.CurrencyExchangeRateKey;
import com.pro.common.service.sitecontent.model.db.Currency;
import com.pro.common.service.sitecontent.service.CurrencyExchangeRateService;
import com.pro.common.service.sitecontent.service.CurrencyService;
import com.pro.framework.api.util.CollUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/inner/currencyExchangeRate")
public class CurrencyExchangeRateInnerController {

    @Autowired
    private CurrencyExchangeRateService service;
    @Autowired
    private CurrencyService currencyService;

    @Operation(summary = "批量更新汇率")
    @PostMapping("/updateBatchById")
    public void updateBatchById(@RequestBody List<CurrencyExchangeRate> entities) {
        List<CurrencyExchangeRate> list = service.list();
        Map<CurrencyExchangeRateKey, CurrencyExchangeRate> map = CollUtils.listToMap(list, CurrencyExchangeRate::toKey);
        // 获取id方便更新
        entities.forEach(entity -> {
            CurrencyExchangeRate exchangeRate = map.get(new CurrencyExchangeRateKey(entity.getFromCurrency(), entity.getToCurrency()));
            if (exchangeRate != null) {
                entity.setId(exchangeRate.getId());
            }
        });
        service.saveOrUpdateBatch(entities);

        Map<EnumCurrency, BigDecimal> currentToUserRateMap = entities.stream().filter(e -> e.getToCurrency().equals(EnumCurrency.USD)).collect(Collectors.toMap(CurrencyExchangeRate::getFromCurrency, CurrencyExchangeRate::getRate));
        Map<EnumCurrency, BigDecimal> usdToCurrentRateMap = entities.stream().filter(e -> e.getFromCurrency().equals(EnumCurrency.USD)).collect(Collectors.toMap(CurrencyExchangeRate::getToCurrency, CurrencyExchangeRate::getRate));
        List<Currency> currencies = currencyService.list();
        List<Currency> currenciesUpdate = currencies.stream().map(currency -> {
            EnumCurrency enumCurrency = EnumCurrency.valueOf(currency.getCode());
            BigDecimal currentToUserRate = currentToUserRateMap.get(enumCurrency);
            BigDecimal usdToCurrentRate = usdToCurrentRateMap.get(enumCurrency);
            if (null == currentToUserRate && null == usdToCurrentRate) {
                return null;
            }
            if (null == currentToUserRate) {
                currentToUserRate = BigDecimal.ONE.divide(usdToCurrentRate, 6, RoundingMode.HALF_UP);
            }
            if (null == usdToCurrentRate) {
                usdToCurrentRate = BigDecimal.ONE.divide(currentToUserRate, 6, RoundingMode.HALF_UP);
            }
            Currency currencyUpdate = new Currency();
            currencyUpdate.setId(currency.getId());
            currencyUpdate.setUsdToCurrentRate(usdToCurrentRate);
            currencyUpdate.setCurrentToUsdRate(currentToUserRate);
            return currencyUpdate;
        }).filter(Objects::nonNull).toList();
        currencyService.updateBatchById(currenciesUpdate);
    }

    @Operation(summary = "批量更新汇率")
    @GetMapping("/getRateMapMap")
    public Map<EnumCurrency, Map<EnumCurrency, BigDecimal>> getRateMapMap() {
        return service.getRateMapMap();
    }

}
